package com.util;

import javafx.util.StringConverter;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class CustomLocalTimeConverter extends StringConverter<LocalTime>{
    private static final DateTimeFormatter CUSTOM_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
    private static final java.util.logging.Logger LOGGER = java.util.logging.Logger.getLogger(CustomLocalTimeConverter.class.getName());

    @Override
    public String toString(LocalTime time) {
        if (time == null) {
            return "";
        }
        return time.format(CUSTOM_FORMATTER);
    }

    @Override
    public LocalTime fromString(String string) {
        if (string == null || string.trim().isEmpty()) { // 去除字符串首尾空格
            return null;
        }
        try {
            String trimmedString = string.trim(); // 去除字符串首尾空格
            LOGGER.info("Attempting to parse time: " + trimmedString); // 添加日志输出
            return LocalTime.parse(trimmedString, CUSTOM_FORMATTER);
        } catch (DateTimeParseException e) {
            LOGGER.severe("Failed to parse time: " + string); // 添加异常日志输出
            return null; // 或者您可以选择抛出异常，根据您应用程序的错误处理策略
        }
    }
}


